package com.ljyh.forum.controller;

import com.ljyh.forum.config.JwtUtil;
import com.ljyh.forum.dto.CommentDTO;
import com.ljyh.forum.entity.Comment;
import com.ljyh.forum.service.CommentService;
import com.ljyh.forum.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/posts/{postId}/comments")
@Tag(name = "评论接口", description = "评论相关接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping
    @Operation(summary = "获取帖子评论列表")
    public ApiResponse<List<CommentDTO>> getComments(@PathVariable Long postId) {
        List<CommentDTO> comments = commentService.getCommentsByPostId(postId);
        return ApiResponse.success("查询成功", comments);
    }

    @PostMapping
    @Operation(summary = "发布评论")
    public ApiResponse<Void> publishComment(
            @PathVariable Long postId,
            @Valid @RequestBody CommentRequest request) {
        
        // 从JWT token中获取用户ID
        Long userId = SecurityUtils.getCurrentUserId(jwtUtil);
        
        if (userId == null) {
            return ApiResponse.error(401, "用户未认证");
        }
        
        boolean success = commentService.publishComment(postId, userId, request.getContent());
        
        if (success) {
            return ApiResponse.success("评论成功", null);
        } else {
            return ApiResponse.error(400, "评论失败，帖子可能不存在");
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论")
    public ApiResponse<Void> deleteComment(@PathVariable Long id) {
        boolean success = commentService.deleteComment(id);
        if (success) {
            return ApiResponse.success("删除成功", null);
        } else {
            return ApiResponse.error(400, "删除失败，评论可能不存在");
        }
    }

    /**
     * 评论请求体
     */
    public static class CommentRequest {
        @NotBlank(message = "评论内容不能为空")
        private String content;

        // getters and setters
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
    }

    /**
     * 统一API响应格式
     */
    public static class ApiResponse<T> {
        private int code;
        private String msg;
        private T data;

        public ApiResponse(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        public static <T> ApiResponse<T> success(String msg, T data) {
            return new ApiResponse<>(200, msg, data);
        }

        public static <T> ApiResponse<T> error(int code, String msg) {
            return new ApiResponse<>(code, msg, null);
        }

        // getters
        public int getCode() { return code; }
        public String getMsg() { return msg; }
        public T getData() { return data; }
    }
}
